package com.lib.net.util;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;

public class FileUtil {
    public static final String IMAGE_PNG_KIND = ".png";
    public static final String IMAGE_JPG_KIND = ".jpg";

    public FileUtil() {
    }

    public static String getFileName(String pathName) {
        int start = pathName.lastIndexOf("/");
        return start != -1 ? pathName.substring(start + 1, pathName.length()) : pathName;
    }

    public static String getExternalStorePath() {
        return isExistExternalStore() ? Environment.getExternalStorageDirectory().getAbsolutePath() : null;
    }

    public static boolean isExistExternalStore() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static boolean isExists(String path) {
        File file = new File(path);
        return file.exists();
    }

    public static File saveImage(String folderPath, String fileName, Bitmap bitmap) {
        return saveImage(folderPath, fileName, bitmap, 0);
    }

    public static File saveImage(String folderPath, String fileName, Bitmap bitmap, int type) {
        String imageType = ".png";
        if (type == 1) {
            imageType = ".jpg";
        }

        try {
            File folder = new File(folderPath);
            createDirectory(folder);
            File imageFile = new File(folderPath + fileName + imageType);
            imageFile.createNewFile();
            FileOutputStream outStream = new FileOutputStream(imageFile);
            if (type == 1) {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            } else {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            }

            outStream.flush();
            outStream.close();
            return imageFile;
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

    public static void createDirectory(File file) {
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            boolean res;
            if (null != file && parentFile.exists()) {
                if (!parentFile.isDirectory()) {
                    parentFile.delete();
                    res = parentFile.mkdir();
                    if (!res) {
                        parentFile.delete();
                    }
                }

                res = file.mkdir();
                if (!res) {
                    file.delete();
                }
            } else {
                createDirectory(file.getParentFile());
                res = file.mkdir();
                if (!res) {
                    file.delete();
                }
            }

        }
    }

    public static boolean mkdirs(File file) {
        return file.exists() ? true : file.mkdirs();
    }

    public static void clearDir(String fileDir) {
        File file = new File(fileDir);
        clearDir(file);
    }

    public static void clearDir(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else {
                String[] childs = file.list();

                for(int i = 0; i < childs.length; ++i) {
                    clearDir(new File(file, childs[i]));
                }

                file.delete();
            }
        }
    }

    public static String getFileExtension(String url) {
        return url.lastIndexOf(".") != -1 && url.lastIndexOf(".") != 0 ? url.substring(url.lastIndexOf(".") + 1) : "";
    }

    public static String getFileExtension(File file) {
        String fileName = file.getName();
        return fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0 ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";
    }

    public static void deleteDirectoryContent(File file) {
        if (file != null) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null && files.length > 0) {
                    File[] var2 = files;
                    int var3 = files.length;

                    for(int var4 = 0; var4 < var3; ++var4) {
                        File f = var2[var4];
                        deleteFile(f);
                    }

                }
            }
        }
    }

    public static void deleteFile(File file) {
        if (file != null) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files == null || files.length <= 0) {
                    file.delete();
                    return;
                }

                File[] var2 = files;
                int var3 = files.length;

                for(int var4 = 0; var4 < var3; ++var4) {
                    File f = var2[var4];
                    deleteFile(f);
                }

                file.delete();
            } else {
                file.delete();
            }

        }
    }

    public static void deleteOtherFile(File file, String[] excludeFile) {
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files == null || files.length <= 0) {
                    file.delete();
                    return;
                }

                File[] var3 = files;
                int var4 = files.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    File f = var3[var5];
                    deleteOtherFile(f, excludeFile);
                }

                file.delete();
            }

        }
    }

    public static void deleteFile(File file, String[] includeFile) {
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files == null || files.length <= 0) {
                    file.delete();
                    return;
                }

                File[] var3 = files;
                int var4 = files.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    File f = var3[var5];
                    deleteFile(f, includeFile);
                }

                file.delete();
            }

        }
    }

    public static long calcSize(File dir) {
        if (dir == null) {
            return 0L;
        } else if (!dir.exists()) {
            return 0L;
        } else if (!dir.isDirectory()) {
            return dir.length();
        } else {
            long sum = 0L;
            File[] files = dir.listFiles();
            File[] var4 = files;
            int var5 = files.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                File file = var4[var6];
                sum += calcSize(file);
            }

            return sum;
        }
    }

    public static String calcSizeString(long size) {
        long original = size;
        float percent = 0.0F;
        String[] tags = new String[]{"B", "KB", "MB", "GB"};

        int i;
        for(i = 0; size >= 1024L; ++i) {
            original = size;
            size >>= 10;
        }

        if (i > 0) {
            percent = (float)(original - (size << 10)) / 1024.0F;
        }

        return String.format("%.2f %s", (float)size + percent, tags[i]);
    }
}
